﻿Public Class Acad_Notes_Form
    Public Dataset_Code As Integer
    Public Field_Code As Integer
    Public Period_ID As Integer
    Public RID As String

    Dim dv As DataView

    Public Sub LoadNotes()
        Dim sql As String

        sql = "select * from Acad_GAG_Notes " & _
                "where Dataset_Code = " & Dataset_Code.ToString & " " & _
                "and Period_ID = " & Period_ID.ToString & " " & _
                "and RID = '" & RID & "' " & _
                "and Field_Code = " & Field_Code.ToString & " order by TheDate desc "

        dv = LoadDataView(sql)

        TheNoteBox.Text = ""
        NotesRepeater.ItemCount = dv.Count

        LoadNote()

    End Sub

    Private Sub AddNote()

        Dim Notecode As String
        Dim SQL As String
        Dim p As New List(Of SqlClient.SqlParameter)


        Notecode = Mid(CurrentUser.Name, 1, 3) & Format(Now(), "ddMMyyyyHHmmss")


        SQL = "insert into Acad_GAG_Notes (Dataset_Code, Period_ID, RID, Field_Code, TheDate, User_Name, Note, Note_Code) Values " & _
            "(@Dataset_Code, @Period_ID, @RID, @Field_Code, @TheDate, @User_Name, @Note, @Note_Code)"


        p.Clear()
        p.Add(New SqlClient.SqlParameter("@Dataset_Code", Dataset_Code))
        p.Add(New SqlClient.SqlParameter("@Period_ID", Period_ID))
        p.Add(New SqlClient.SqlParameter("@RID", RID))
        p.Add(New SqlClient.SqlParameter("@Field_Code", Field_Code))
        p.Add(New SqlClient.SqlParameter("@TheDate", Now))
        p.Add(New SqlClient.SqlParameter("@User_Name", CurrentUser.Name))
        p.Add(New SqlClient.SqlParameter("@Note", "Enter your note here"))
        p.Add(New SqlClient.SqlParameter("@Note_Code", Notecode))

        RunNonQueryParams(SQL, p)

        LoadNotes()

    End Sub

    Private Sub EditNote()

        Dim SQL As String
        Dim p As New List(Of SqlClient.SqlParameter)
        Dim Notecode As String
        Dim i As Integer

        i = 0

        If NotesRepeater.ItemCount = 0 Then Exit Sub

        If NotesRepeater.CurrentItemIndex <> 0 Then Exit Sub

        If Trim(dv(i)("User_Name").ToString.ToUpper) <> Trim(CurrentUser.Name.ToUpper) Then Exit Sub

        If dv(i)("Note").ToString = TheNoteBox.Text Then Exit Sub

        Notecode = dv(i)("Note_Code").ToString

        SQL = "update Acad_GAG_Notes set TheDate = @TheDate, Note = @Note where Note_Code = @Note_Code"

        p.Clear()
        p.Add(New SqlClient.SqlParameter("@TheDate", Now))
        p.Add(New SqlClient.SqlParameter("@Note", TheNoteBox.Text))
        p.Add(New SqlClient.SqlParameter("@Note_Code", Notecode))

        RunNonQueryParams(SQL, p)

        dv(i)("TheDate") = Now
        dv(i)("Note") = TheNoteBox.Text

    End Sub

    Private Sub Acad_Notes_Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub NotesRepeater_ItemValueNeeded(ByVal sender As System.Object, ByVal e As Microsoft.VisualBasic.PowerPacks.DataRepeaterItemValueEventArgs) Handles NotesRepeater.ItemValueNeeded
        If e.ItemIndex >= 0 Then

            Dim s As String

            s = Format(dv(e.ItemIndex)("TheDate"), "d MMM yy").ToString
            s = s & vbNewLine
            s = s & Format(dv(e.ItemIndex)("TheDate"), "hh:mm").ToString
            s = s & vbNewLine
            s = s & Trim(dv(e.ItemIndex)("User_Name").ToString)

            Select Case e.Control.Name
                Case "NoteLabel"
                    e.Value = s
            End Select


        End If
    End Sub

    Private Sub LoadNote()
        Dim i As Integer

        i = NotesRepeater.CurrentItemIndex

        If i < 0 Then Exit Sub

        TheNoteBox.Text = dv(i)("Note").ToString


        'set the state
        If NotesRepeater.ItemCount = 0 Then
            TheNoteBox.ReadOnly = True
            Exit Sub
        End If

        If i <> 0 Then
            TheNoteBox.ReadOnly = True
            Exit Sub
        End If

        If Trim(dv(i)("User_Name").ToString.ToUpper) <> Trim(CurrentUser.Name.ToUpper) Then
            TheNoteBox.ReadOnly = True
        Else
            TheNoteBox.ReadOnly = False
        End If

    End Sub

    Private Sub DeleteNote()
        Dim SQL As String
        Dim p As New List(Of SqlClient.SqlParameter)
        Dim Notecode As String
        Dim i As Integer

        i = 0

        If NotesRepeater.ItemCount = 0 Then Exit Sub

        If NotesRepeater.CurrentItemIndex <> 0 Then Exit Sub

        If Trim(dv(i)("User_Name").ToString.ToUpper) <> Trim(CurrentUser.Name.ToUpper) Then Exit Sub

        If MessageBox.Show("Are you sure you want to delete this note?", "Delete note?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then
            Exit Sub
        End If

        Notecode = dv(i)("Note_Code").ToString

        SQL = "delete from Acad_GAG_Notes where Note_Code = @Note_Code"

        p.Clear()

        p.Add(New SqlClient.SqlParameter("@Note_Code", Notecode))

        RunNonQueryParams(SQL, p)

        LoadNotes()

    End Sub

    Private Sub NotesRepeater_CurrentItemIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NotesRepeater.CurrentItemIndexChanged
        
        LoadNote()

        If NotesRepeater.ItemCount = 0 Then
            SaveButton.Visible = False
            DeleteButton.Visible = False
            Exit Sub
        End If


        Dim i As Integer

        i = NotesRepeater.CurrentItemIndex

        If i <> 0 Then
            SaveButton.Visible = False
            DeleteButton.Visible = False

            Exit Sub
        End If

        If Trim(dv(i)("User_Name").ToString.ToUpper) <> Trim(CurrentUser.Name.ToUpper) Then
            SaveButton.Visible = False
            DeleteButton.Visible = False
        Else
            SaveButton.Visible = True
            DeleteButton.Visible = True
        End If


    End Sub


    Private Sub AddButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddButton.Click
        AddNote()
    End Sub


    Private Sub SaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveButton.Click
        EditNote()

    End Sub

    Private Sub TheNoteBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TheNoteBox.TextChanged
       
    End Sub

    Private Sub DeleteButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteButton.Click
        DeleteNote()
    End Sub
End Class